package com.base.service.impl;

import java.util.Date;
import java.util.List;

import com.base.service.FlowaudithistoryService;
import com.common.bean.Flowaudithistory;
import com.common.util.AbstractManager;
import com.common.util.DateHelper;
import com.common.util.PagerModel;

public class FlowaudithistoryServiceImpl extends AbstractManager implements
		FlowaudithistoryService {

	public void addFlowaudithistory(Flowaudithistory flowaudithistory) {
		String hql = "select count(*) from Flowaudithistory";
		
		long count = 0l;
		count = (Long) this.getHibernateTemplate().find(hql).get(0);
		count +=1l;
		
		Long num = Long.parseLong(DateHelper.dateToStr(new Date(), "yyyyMMdd"));
		num *= 1000000L;		
		num += count;
		
		long id = flowaudithistory.getDatadictByFlowflagno().getTypeid();//暂时获取字典id为标识，可取name（BL、RK,CK等）
		id %= 100;//不超过4位  主键在16位以内
		
		StringBuilder flowid = new StringBuilder();
		flowid.append(id + "");
		flowid.append(num + "");
		
		flowaudithistory.setFlowid(flowid.toString());
		flowaudithistory.setAuditdate(new Date());
		this.getHibernateTemplate().save(flowaudithistory);
	}

	@SuppressWarnings("unchecked")
	public List<Flowaudithistory> findAllFlowaudithistory() {
		String hql = "from Flowaudithistory order by flowflag,flowno,flowid";
		return this.getHibernateTemplate().find(hql);
	}

	public Flowaudithistory findFlowaudithistory(String flowid) {
		return (Flowaudithistory) this.getHibernateTemplate().load(Flowaudithistory.class, flowid);
	}

	public PagerModel findFlowaudithistoryForPager() {
		String hql = "from Flowaudithistory order by flowflag,flowno,flowid";
		return searchPaginated(hql);
	}
	
	public PagerModel findFlowaudithistoryForPager(long flowflag, String flowno) {
		String hql = "from Flowaudithistory flow where flow.datadictByFlowflagno.typeid = ? and flow.flowno = ?  order by flow.flowid";
		return searchPaginated(hql,new Object[]{flowflag,flowno});
	}

	public void removeFlowaudithistory(String flowid) {
		Flowaudithistory flowaudithistory = null;
		flowaudithistory = (Flowaudithistory) this.getHibernateTemplate().load(Flowaudithistory.class, flowid);
		if(flowaudithistory != null) {
			this.getHibernateTemplate().delete(flowaudithistory);
		}
	}

	public void updateFlowaudithistory(Flowaudithistory flowaudithistory) {
		this.getHibernateTemplate().update(flowaudithistory);
	}

	@SuppressWarnings("unchecked")
	public List<Flowaudithistory> findAllFlowaudithistoryWith(long flowflag, String flowno) {
		String hql = "from Flowaudithistory flow where flow.datadictByFlowflagno.typeid = ? and flow.flowno = ? order by flow.flowid";
		return this.getHibernateTemplate().find(hql,new Object[]{flowflag,flowno});
	}

}
